﻿using FreeSql;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using MiniExcelLibs.Attributes;
using MiniExcelLibs.OpenXml;
using System.Web;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

//创建时间：2025-06-24
namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 记录工件加工过程的详细信息
    /// </summary>
    [Verify]
    [Route("business/CksProductionRecord")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksProductionRecordController : BaseController
    {
        /// <summary>
        /// 记录工件加工过程的详细信息接口
        /// </summary>
        private readonly ICksProductionRecordService _CksProductionRecordService;

        public CksProductionRecordController(ICksProductionRecordService CksProductionRecordService)
        {
            _CksProductionRecordService = CksProductionRecordService;
        }

        /// <summary>
        /// 查询记录工件加工过程的详细信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "cksproductionrecord:list")]
        public IActionResult QueryCksProductionRecord([FromQuery] CksProductionRecordQueryDto parm)
        {
            var response = _CksProductionRecordService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询记录工件加工过程的详细信息详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "cksproductionrecord:query")]
        public IActionResult GetCksProductionRecord(long Id)
        {
            var response = _CksProductionRecordService.GetInfo(Id);

            var info = response.Adapt<CksProductionRecordDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加记录工件加工过程的详细信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "cksproductionrecord:add")]
        [Log(Title = "记录工件加工过程的详细信息", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCksProductionRecord([FromBody] CksProductionRecordDto parm)
        {
            var modal = parm.Adapt<CksProductionRecord>().ToCreate(HttpContext);

            var response = _CksProductionRecordService.AddCksProductionRecord(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新记录工件加工过程的详细信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "cksproductionrecord:edit")]
        [Log(Title = "记录工件加工过程的详细信息", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCksProductionRecord([FromBody] CksProductionRecordDto parm)
        {
            var modal = parm.Adapt<CksProductionRecord>().ToUpdate(HttpContext);
            var response = _CksProductionRecordService.UpdateCksProductionRecord(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除记录工件加工过程的详细信息
        /// </summary>
        /// <returns></returns>
        [HttpDelete("delete/{ids}")]
        [ActionPermissionFilter(Permission = "cksproductionrecord:delete")]
        [Log(Title = "记录工件加工过程的详细信息", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCksProductionRecord([FromRoute] string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_CksProductionRecordService.Delete(idArr));
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("ExportData")]
        public IActionResult ExportData([FromQuery] CksProductionRecordQueryDto parm)
        {
            var _fsql = InternalApp.Fsql;
            using (var ObjectCo = _fsql.Ado.MasterPool.Get())
            {
                var conn = ObjectCo.Value;
                var exportData = conn.Select<CksProductionRecord>()
                     .WhereIf(parm.BeginTime != null, x => x.CreatedTime >= parm.BeginTime.Value && x.ASideStartTime <= parm.EndTime.Value)
                     .WhereIf(!parm.CoverCode.IsEmpty(), x => x.CoverCode == parm.CoverCode)
                     .WhereIf(!parm.ProductCode.IsEmpty(), x => x.ProductCode == parm.ProductCode)
                     .OrderByDescending(a=>a.Id)
                     .ToList();



                List<DynamicExcelColumn> excelColumns = new List<DynamicExcelColumn>() {
                    new DynamicExcelColumn("SerialNumber") {Name="流水号", Width = 20 },
                    new DynamicExcelColumn("CoverCode") {Name="盖板码", Width = 20 },
                    new DynamicExcelColumn("ProductCode") {Name="成品码", Width = 20 },
                    new DynamicExcelColumn("BarcodeGrade") {Name="条码等级", Width = 20 },
                    new DynamicExcelColumn("ProcessingStatusText") {Name="加工状态", Width = 20 },
                    new DynamicExcelColumn("ASideMachineNumber") {Name="A面机床编号", Width = 20 },
                    new DynamicExcelColumn("ASideToolingNumber") {Name="A面工装编号", Width = 20 },
                    new DynamicExcelColumn("ASideLoadingTime") {Name="A面投入时间", Width = 20 , Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("ASideStartTime") {Name="A面开始时间", Width = 20 , Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("ASideEndTime") {Name="A面结束时间", Width = 20 , Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("ASideUnloadingTime") {Name="A面取出时间", Width = 20, Format = "yyyy-MM-dd HH:mm:ss" },
                    new DynamicExcelColumn("MarkingLoadingTime") {Name="打标投入时间", Width = 20, Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("MarkingStartTime") {Name="打标开始时间", Width = 20,  Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("MarkingCompletedTime") {Name="打标完成时间", Width = 20,  Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("MarkingGradingTime") {Name="打标评级时间", Width = 20,  Format = "yyyy-MM-dd HH:mm:ss" },
                    new DynamicExcelColumn("BSideMachineNumber") {Name="B面机床编号", Width = 20},
                    new DynamicExcelColumn("BSideToolingNumber") {Name="B面工装编号", Width = 20 },
                    new DynamicExcelColumn("BSideLoadingTime") {Name="B面投入时间", Width = 20,  Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("BSideStartTime") {Name="B面开始时间", Width = 20,  Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("BSideEndTime") {Name="B面结束时间", Width = 20,  Format = "yyyy-MM-dd HH:mm:ss"},
                    new DynamicExcelColumn("BSideUnloadingTime") {Name="B面取出时间", Width = 20 , Format = "yyyy-MM-dd HH:mm:ss"},
                   
                };
                var config = new OpenXmlConfiguration
                {

                    DynamicColumns = excelColumns.ToArray()
                };

                var stream = new MemoryStream();
                stream.SaveAs(exportData ,configuration:config);
                stream.Seek(0, SeekOrigin.Begin);

                Response.Headers.Append("Access-Control-Expose-Headers", "Content-Disposition");
                return File(stream,
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    HttpUtility.UrlEncode($"MES-Export-{DateTime.Now.ToString("MMddHHmmss")}.xlsx"));


            }
        }
    }
}